package com.showcai.controller.distributor;

import com.github.pagehelper.PageInfo;
import com.showcai.controller.base.BaseController;
import com.showcai.def.Constant;
import com.showcai.module.Distributor;
import com.showcai.myRunTime.MyRunTimeExcption;
import com.showcai.result.ResultObj;
import com.showcai.service.ActivityService;
import com.showcai.service.DistributorService;
import com.showcai.tools.RandomCode;
import com.showcai.tools.aliUpload.AliUploadTools;
import com.showcai.util.FileTools;
import com.showcai.util.QRCodeUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Baggio on 2018-7-31.
 */
@Controller
@RequestMapping(value = "distributor")
public class DistributorController extends BaseController {
    @Autowired
    DistributorService distributorService;
    @Autowired
    ActivityService activityService;

    @RequestMapping("/distributorList")
    public ModelAndView distributorList(Integer page, Integer pageSize) {
        ModelAndView m = new ModelAndView("/distributor/distributorList");
        Map<String, String> map = this.showRequestParameters();
        if (page == null || page == 0) {
            page = Constant.PAGENUM;
        }
        // 设置每页记录数
        if (pageSize == null || pageSize == 0) {
            pageSize = Constant.PAGESIZENum;
        }
        map.put("page", String.valueOf(page));
        map.put("pageSize", String.valueOf(pageSize));
        PageInfo pageinfo = distributorService.getDistributorList(map);
        m.addObject("pageinfo", pageinfo);
        m.addObject("bean", map);
        return m;
    }

    @RequestMapping("/distributor")
    public ModelAndView distributor(Long tid) {


        ModelAndView m = new ModelAndView("/distributor/distributor");
        if (tid != null) {
            Distributor distributor = distributorService.getDistributorById(tid);
            m.addObject("distributor", distributor);
        }
     PageInfo pageInfo= activityService.getActivityList(new HashMap<String,String>(){{put("pageSize","10000");}});
        m.addObject("activityList", pageInfo.getList());
        return m;
    }

    @RequestMapping("/distributorStatistics")
    public ModelAndView distributorStatistics() {
        ModelAndView m = new ModelAndView("/distributor/distributorStatistics");
        Map<String, String> map = super.showRequestParameters();

        String startDate = map.get("startDate");
        String endDate = map.get("startDate");
        if (StringUtils.isNotBlank(endDate)) {
            startDate = startDate + " 00:00:00";
            map.put("startDate", startDate);
        }
        if (StringUtils.isNotBlank(endDate)) {
            endDate = endDate + " 23:59:59";
            map.put("endDate", endDate);
        }
        PageInfo pageinfo = distributorService.distributorStatistics(map);
        Map map1 = distributorService.distributorStatisticsALl(map);

        m.addObject("StatisticsALl", map1);
        m.addObject("bean", map);
        m.addObject("pageInfo", pageinfo);
        return m;
    }


    @RequestMapping(value = "/getSuperiorDistributor", method = RequestMethod.POST)
    @ResponseBody
    public String getSuperiorDistributor(Byte distributorType) {
        Map<String, String> map = new HashMap<>();
        ResultObj resultObj = new ResultObj();
        if (distributorType != null && distributorType > 1) {
            map.put("distributorType", String.valueOf(distributorType - 1));
            List<Distributor> distributorList = distributorService.selectDistributorList(map);
            resultObj.setData(distributorList);
        }
        logger.info("resultObj====>>>>" + resultObj.toJson());
        return resultObj.toJson();
    }

    @RequestMapping("/modifyDistributor")
    @ResponseBody
    public String modifyDistributor(Distributor distributor) {
        Map<String, String> map = new HashMap<>();
        ResultObj resultObj = new ResultObj();

        String distributorCode = RandomCode.getCode(8);
        distributor.setCode(distributorCode);
        distributor.setCreateTime(new Date());
        String sourceQRCode = "https://www.showcai.com/apply/apply.html?activityId=1&distributorCode=" + distributorCode;

        //上传文件
        String key = "showcai/distributorCode/" + distributorCode + FileTools.PICTURE_SUFFIX_JPG;
        try {
            BufferedImage bufferedImage = QRCodeUtil.getQRCode(sourceQRCode);
            AliUploadTools.AliUploadBufferedImage(bufferedImage, true, key);

            distributor.setSourceQRCode(AliUploadTools.PICURLADDRESS + key);
        } catch (IOException e) {
            e.printStackTrace();
            throw new MyRunTimeExcption(e.getMessage());
        }


        distributorService.modifyDistributorInfo(distributor);
        logger.info("resultObj====>>>>" + resultObj.toJson());
        return resultObj.toJson();
    }

    @RequestMapping(value = "/printDistributorList")
    public ModelAndView printDistributorList(HttpServletRequest request) {
        ModelAndView mov = new ModelAndView();
        mov.setViewName("/distributor/printDistributorList");
        Map<String, String> map = this.showRequestParameters();
        List<Distributor> distributorList = distributorService.selectDistributorList(map);
        mov.addObject("distributorList", distributorList);
        return mov;

    }
}
